package com.atguigu.gulimail.product.web;

import com.atguigu.gulimail.product.entity.CategoryEntity;
import com.atguigu.gulimail.product.service.CategoryService;
import com.atguigu.gulimail.product.vo.Catalog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author fangy
 * @date 2022-09-25 16:33
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping({"/","/index."})
    public String index(Model model){

        List<CategoryEntity> categoryOneList = categoryService.getOneCategory();

        model.addAttribute("categoryOneList",categoryOneList);

        return "index";

    }


    @ResponseBody
    @RequestMapping("/index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatelogJson(Model model){

        Map<String, List<Catalog2Vo>> categoryOneList = categoryService.getCatelogJson();

        return categoryOneList;

    }


    @ResponseBody
    @RequestMapping("/read")
    public String read(Model model){

        RReadWriteLock rw_lock = redissonClient.getReadWriteLock("Rw_lock");
        RLock rLock = rw_lock.readLock();

        rLock.lock();

        String fangyi = "";

        try {
            Thread.sleep(30000);
            fangyi = stringRedisTemplate.opsForValue().get("fangyi");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }

        return fangyi;

    }


    @ResponseBody
    @RequestMapping("/write")
    public String wirte(Model model){

        String s = UUID.randomUUID().toString();

        RReadWriteLock rw_lock = redissonClient.getReadWriteLock("Rw_lock");

        RLock rLock = rw_lock.writeLock();
        rLock.lock();

        try {
            Thread.sleep(30000);
            stringRedisTemplate.opsForValue().set("fangyi",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }

        return s;

    }


    /**
     *  CountDownLatch   闭锁
     */
    @ResponseBody
    @RequestMapping("/door")
    public String CountDownLatch_door(Model model){

        RCountDownLatch lock = redissonClient.getCountDownLatch("fangyi");
        lock.trySetCount(5);
        try {
            lock.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     *  CountDownLatch   闭锁
     */
    @ResponseBody
    @RequestMapping("/gogogo/{id}")
    public String CountDownLatch(@PathVariable("id") Long id){

        RCountDownLatch countDownLatch = redissonClient.getCountDownLatch("fangyi");

        countDownLatch.countDown();

        return id+"班放假了";
    }



    /**
     *  Semaphore   信号量
     */
    @ResponseBody
    @RequestMapping("/park")
    public String park(){

        RSemaphore park = redissonClient.getSemaphore("park");

        boolean b = false;

        try {
            b = park.tryAcquire();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "ok->"+b;
    }

    /**
     *  Semaphore   信号量
     */
    @ResponseBody
    @RequestMapping("/go")
    public String go(){

        RSemaphore park = redissonClient.getSemaphore("park");
        park.release();


        return "停车成功";
    }

}




































